package com.dyzs.client;

import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

import com.dyzs.util.Constants;
import com.dyzs.util.ReadProperties;
import com.dyzs.util.WSUtils;

public class LoginClient {

	public String getData(String userName, String userWord) throws Exception {
		Map<String, Object> params = new TreeMap<String, Object>();
		params.put("arg0", userName);
		params.put("arg1", userWord);
		params.put("arg2", ReadProperties.getPropertyByStr("client.version"));
		try {
			String resultXml = WSUtils.callWS("loginForAndroid", params,Constants.LoginURL);
			return resultXml;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e.getMessage(), e);
		}
	}

	public Map<String, String> pullXml(String resultXml) throws Exception {
		if (resultXml.equals("W")) {
			throw new Exception("服务端异常！");
		} else {
			Map map = new HashMap<String, String>();
			try {
				ByteArrayInputStream bin = new ByteArrayInputStream(resultXml.getBytes());
				InputStreamReader in = new InputStreamReader(bin);
				XmlPullParser parser = Xml.newPullParser();
				parser.setInput(in);
				int event = parser.getEventType();
				while (event != XmlPullParser.END_DOCUMENT) {
					switch (event) {
					case XmlPullParser.START_TAG:
						if (map != null) {
							if ("result".equals(parser.getName())) {
								map.put("result", parser.nextText());
							} else if ("value".equals(parser.getName())) {
								map.put("value", parser.nextText());
							} else if ("gx".equals(parser.getName())) {
								map.put("gx", parser.nextText());
							}
						}
						break;
					}
					event = parser.next();
				}

			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception("解析失败！", e);
			}
			return map;

		}

	}

}
